package com.estore.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.estore.converter.DateTime;
import com.estore.converter.ImageResize;
import com.estore.entity.Categories;
import com.estore.entity.Discount;
import com.estore.entity.MainCategories;
import com.estore.entity.Product_Image;
import com.estore.info.BrandInfo;
import com.estore.info.AgentProductInfo;
import com.estore.info.ProductInfo;
import com.estore.info.ShopInfo;
import com.estore.info.SubCatInfo;
import com.estore.manager.ProductManager;
import com.estore.manager.ProfileManager;
import com.estore.manager.ShopManager;
import com.estore.model.ProductModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends ActionSupport implements
		ModelDriven<ProductModel>, ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5625570598603354403L;
	ProductModel productModel = new ProductModel();
	ProductManager productManager;
	ShopManager shopManager;
	ProfileManager profileManager;

	public HttpServletRequest request;
	private List<SubCatInfo> listSubCat;
	private List<String> listAutoComplete;

	// auto complete data
	public String autoComplete(){
		productModel.setAutoComplete(productManager.getAutoCompleteList());
		String[] tokens = productManager.getAutoCompleteList().split(",");
		List<String> list = new ArrayList<String>();
		for(String record : tokens){
			list.add(record);
		}
		listAutoComplete = list;
		return SUCCESS;
	}
	
	// search product on homepage
	public String searchProduct() {
		productModel.setExactlyResultEmpty("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {
			String searchText = productModel.getSearchText();
			if (searchText != null) {
				if(searchText.equals("")){
					List<ProductInfo> listProduct = productManager.getAllProduct(1);
					List<ProductInfo> listProductResult = new ArrayList<ProductInfo>();
					for(ProductInfo record : listProduct){
						AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
						if(shopManager.getLowestPrice(Integer.parseInt(record.getProductID())) != null){
							
							record.setDiscount(agentProduct.getDiscount());
							record.setDiscountPercent(agentProduct.getDiscountPercent());
						}
						if(agentProduct != null){
							record.setSellingPrice(agentProduct.getSellingPrice());
							record.setListID(agentProduct.getListID());
						}
						listProductResult.add(record);
					}
					productModel.setListProduct(listProductResult);
					setListSubCat(productManager.listSubCatInfo());
					productModel.setSearchText("");
					return "fullsearch";
				}
				List<ProductInfo> listFullTextSearchResult = new ArrayList<ProductInfo>();
				List<ProductInfo> listExactlyResult = new ArrayList<ProductInfo>();
				List<ProductInfo> listFullTextSearch = productManager.fullTextSearchResult(searchText);
				for(ProductInfo record : listFullTextSearch){
					AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
					if(shopManager.getLowestPrice(Integer.parseInt(record.getProductID())) != null){
						
						record.setDiscount(agentProduct.getDiscount());
						record.setDiscountPercent(agentProduct.getDiscountPercent());
					}
					if(agentProduct != null){
						record.setSellingPrice(agentProduct.getSellingPrice());
						record.setListID(agentProduct.getListID());
					}
					listFullTextSearchResult.add(record);
				}
				List<ProductInfo> listExactly = productManager.exactlySearchResult(searchText);
				for(ProductInfo record : listExactly){
					AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
					if(shopManager.getLowestPrice(Integer.parseInt(record.getProductID())) != null){
						
						record.setDiscount(agentProduct.getDiscount());
						record.setDiscountPercent(agentProduct.getDiscountPercent());
					}
					if(agentProduct != null){
						record.setSellingPrice(agentProduct.getSellingPrice());
						record.setListID(agentProduct.getListID());
					}
					listExactlyResult.add(record);
				}
				productModel.setFullTextResult(listFullTextSearchResult);
				productModel.setExactlyResult(listExactlyResult);
				productModel.setFullTextResultSize(listFullTextSearchResult.size());
				productModel.setExactlyResultSize(listExactlyResult.size());
				if(listExactlyResult.isEmpty()){
					productModel.setExactlyResultEmpty("Không có kết quả cho từ khóa: ");
				}
				productModel.setTextDisplayOnSearch(searchText);
				// StringTokenizer st =new StringTokenizer(searchText);
				// System.out.println("tokens count: " + st.countTokens());
				// // iterate through st object to get more tokens from it
				// while (st.hasMoreElements()) {
				// String token = st.nextElement().toString();
				// System.out.println("token = " + token);
				// }
			}
			setListSubCat(productManager.listSubCatInfo());
			productModel.setSearchText("");
			productModel.setAutoComplete(productManager.getAutoCompleteList());
			return SUCCESS;
		} else {
			return "banned";
		}
	}

	// add product Image
	public String addProductImage() {
		Integer imgType = null;
		productModel.setProductInfo(null);
		productModel.setListFileSubmit(null);
		String productID = request.getParameter("productID");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {
			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1)
					|| session.get("role").equals(2)
					|| session.get("role").equals(5)) {
				List<File> fileUpload = productModel.getImgUpload();
				List<String> fileUploadName = productModel
						.getImgUploadFileName();
				List<String> listFileName = new ArrayList<String>();
				DateTime dateTime = new DateTime();

				if (session.get("role").equals(1)
						|| session.get("role").equals(2)) {
					imgType = 0;
				} else if (session.get("role").equals(5)) {
					ShopInfo shopInfo = (ShopInfo) session.get("shopInfo");
					imgType = shopInfo.getAgentID();
				}
				if (fileUpload != null && fileUploadName != null) {
					for (int i = 0; i < fileUpload.size(); i++) {
						String fileName = dateTime.addImgDate()
								+ fileUploadName.get(i);
						String contextPath = request.getRealPath("/");
						String path = contextPath + "img/" + fileName;
						listFileName.add("img/" + fileName);
						try {
							File f = new File(path);
							BufferedImage originalImage = ImageIO
									.read(fileUpload.get(i));
							int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
									: originalImage.getType();
							BufferedImage resizeImage = ImageResize
									.resizeImage(originalImage, type);
							ImageIO.write(resizeImage, "jpg", f);
							// FileUtils.copyFile(fileUpload.get(i), f);
						} catch (IOException e) {
							System.out.println(e.getMessage());
						}
					}
					List<String> listName = new ArrayList<String>();
					for (String record : listFileName) {
						boolean check = true;
						for (String record1 : listName) {
							if (record.equals(record1)) {
								check = false;
							}
						}
						if (check == true) {
							listName.add(record);
						}
					}
					List<Product_Image> listProductImg = new ArrayList<Product_Image>();
					for (String record : listName) {
						Product_Image productImg = new Product_Image();
						productImg.setProductID(Integer.parseInt(productID));
						productImg.setMainImg(0);
						productImg.setLinkImg(record);
						productImg.setImgType(imgType);
						listProductImg.add(productImg);
					}
					productManager.addNewImg(listProductImg);
					productModel.setImgUpload(null);
					productModel.setImgUploadFileName(null);
					if (!productManager.checkMainImg(productID,
							imgType.toString())) {
						productManager.setMainImg(productID, imgType.toString());
					}
				}
				if(productID == null){
					productID = productModel.getProductID();
				}
				productModel.setProductInfo(productManager.getProductInfo(
						Integer.parseInt(productID), imgType));
				ProductInfo info = productManager.getProductInfo(Integer.parseInt(productID), imgType);
				List<String> linkImg = info.getListLinkImg();
				Integer i = 5;
				for (String record : linkImg) {
					i--;
				}
				List<String> listFileSubmit = new ArrayList<String>();
				for (Integer j = 0; j < i; j++) {
					listFileSubmit.add("aa");
				}
				if (listFileSubmit != null) {
					productModel.setListFileSubmit(listFileSubmit);
				}
				if (session.get("role").equals(5)) {
					return "shop";
				}
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// delete product image
	public String deleteProductImg() {
		productModel.setProductInfo(null);
		productModel.setListFileSubmit(null);
		Dispatcher.getInstance().getConfigurationManager().reload();
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1)
					|| session.get("role").equals(2)
					|| session.get("role").equals(5)) {
				String productID = request.getParameter("productID");
				String link = request.getParameter("linkImg");
				if (productID != null && link != null) {
					// delete image db
					productManager.deleteImage(link, productID);
					// delete sv's image
					try {
						File f = new File(request.getRealPath("/") + link);
						f.delete();
					} catch (Exception e) {

					}
					Integer imgType = null;
					if (session.get("role").equals(1)
							|| session.get("role").equals(2)) {
						imgType = 0;
					} else if (session.get("role").equals(5)) {
						ShopInfo shopInfo = (ShopInfo) session.get("shopInfo");
						imgType = shopInfo.getAgentID();
					}
					if (!productManager.checkMainImg(productID, imgType.toString())) {
						productManager.setMainImg(productID, imgType.toString());
					}
					productModel.setProductInfo(productManager.getProductInfo(Integer.parseInt(productID), imgType));
					ProductInfo info = productManager.getProductInfo(Integer.parseInt(productID), imgType);
					List<String> linkImg = info.getListLinkImg();
					Integer i = 5;
					for (String record : linkImg) {
						i--;
					}
					List<String> listFileSubmit = new ArrayList<String>();
					for (Integer j = 0; j < i; j++) {
						listFileSubmit.add("aa");
					}
					if (listFileSubmit != null) {
						productModel.setListFileSubmit(listFileSubmit);
					}
					productModel.setImgUpload(null);
					productModel.setImgUploadFileName(null);
				}
				if (session.get("role").equals(5)) {
					return "shop";
				}
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// edit product detail
	public String editProductDetail() {
		productModel.setErrorAddProduct("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1)
					|| session.get("role").equals(2)) {
				if (productModel.getProductInfo() != null) {
					ProductInfo productInfo = productModel.getProductInfo();
					if (!productInfo.getProductName().equals("")
							&& productModel.getCatID() != 0
							&& productModel.getBrandID() != 0) {
						if (!(productManager.checkCurrentProductName(
								productInfo.getProductName(), productInfo.getProductID()))
								&& productManager.checkProduct(productInfo.getProductName())) {
							productModel.setErrorAddProduct("Sản Phẩm đã tồn tại");
							addProductPageInfo();
						} else {
							ProductInfo info = new ProductInfo(productInfo.getProductName(),
									productModel.getCatID(), productModel.getBrandID(),
									productInfo.getProductID(), productInfo.getContentOfWeb());
							productManager.editProductDetail(info);
							productModel.setErrorAddProduct("Thay đổi thông tin Sản Phẩm thành công");
							addProductPageInfo();
							productModel.setProductName(null);
						}
					} else {
						productModel
								.setErrorAddProduct("Xin kiểm tra lại thông tin Sản Phẩm");
						addProductPageInfo();
					}
				}
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// add product
	public String addProduct() {
		productModel.setErrorAddProduct("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1)
					|| session.get("role").equals(2)) {
				productModel.setErrorAddProduct(null);
				String fileName = "";
				List<String> listFileName = new ArrayList<String>();
				DateTime dateTime = new DateTime();
				List<File> fileUpload = productModel.getImgUpload();
				List<String> fileUploadName = productModel.getImgUploadFileName();
				if(fileUploadName == null || fileUploadName.size() > 5){
					productModel.setErrorAddProduct("Bạn chỉ được đăng tối đa 5 hình ảnh");
					return SUCCESS;
				}
				if (productModel.getProductName() != null && productModel.getCatID() != 0 && productModel.getBrandID() != 0) {
					if (productManager.checkProduct(productModel.getProductName())) {
						productModel.setErrorAddProduct("Sản Phẩm đã tồn tại");
						addProductPageInfo();
					} else {
						if (fileUpload != null && fileUploadName != null) {
							for (int i = 0; i < fileUpload.size(); i++) {
								fileName = dateTime.addImgDate() + fileUploadName.get(i);
								String contextPath = request.getRealPath("/");
								String path = contextPath + "img/" + fileName;
								listFileName.add("img/" + fileName);
								try {
									File f = new File(path);
									BufferedImage originalImage = ImageIO.read(fileUpload.get(i));
									int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
									BufferedImage resizeImage = ImageResize.resizeImage(originalImage, type);
									ImageIO.write(resizeImage, "jpg", f);
									// FileUtils.copyFile(fileUpload.get(i), f);
								} catch (IOException e) {
									System.out.println(e.getMessage());
								}
							}
						}
						List<String> listName = new ArrayList<String>();
						for (String record : listFileName) {
							boolean check = true;
							for (String record1 : listName) {
								if (record.equals(record1)) {
									check = false;
								}
							}
							if (check == true) {
								listName.add(record);
							}
						}
						ProductInfo info = new ProductInfo(productModel.getProductName(),
								productModel.getCatID(),productModel.getBrandID(),
								productModel.getContentOfWeb(), dateTime.currentDate(), listName);
						productManager.addProduct(info);
						productModel.setErrorAddProduct("Thêm Sản Phẩm thành công");
						addProductPageInfo();
						productModel.setListCategories(productManager.listAllCategories());
						productModel.setContentOfWeb(null);
						productModel.setProductName(null);
						productModel.setImgUpload(null);
						productModel.setImgUploadFileName(null);
					}
				} else {
					productModel.setErrorAddProduct("Xin kiểm tra lại thông tin Sản Phẩm");
					addProductPageInfo();
				}
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// show detail product
	public String clickDetailProduct() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {

			String id = request.getParameter("productID");
			if (id != null) {
				Integer productID = Integer.parseInt(id);
				if (productID != null) {
					ProductInfo productInfo = productManager.getProductInfo(
							productID, 0);
					if (session.get("role") != null) {
						if (session.get("role").equals(4)) {
							String userID = session.get("userID").toString();
							if (profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 1)
									&& profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 2)) {
								productInfo.setFavorite(3);
							} else if (profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 1)
									&& !profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 2)) {
								productInfo.setFavorite(1);
							} else if (!profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 1)
									&& profileManager.checkFavoriteProduct(productID, Integer.parseInt(userID), 2)) {
								productInfo.setFavorite(2);
							}
						}
						else if(session.get("role").equals(5)){
							ShopInfo shopInfo = (ShopInfo) session.get("shopInfo");
							if(shopManager.checkListAvail(productID, shopInfo.getAgentID())){
								productInfo.setFavorite(4);
							}
						}
					}
					listSubCat = productManager.listSubCatInfo();
					productModel.setProductInfo(productInfo);
					productModel.setListAgentProduct(shopManager.getListAgentSellingProduct(productID));
				}
			}
			return SUCCESS;
		} else {
			return "banned";
		}
	}

	// list product by catid
	public String listProductByCat() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			String id = request.getParameter("catID");
			if (id != null) {
				Integer catID = Integer.parseInt(id);
				if (catID != null) {
					List<ProductInfo> listProduct = productManager.getProductByCat(catID);
					List<ProductInfo> listProductResult = new ArrayList<ProductInfo>();
					for(ProductInfo record : listProduct){
						AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
						if(agentProduct != null){
							record.setSellingPrice(agentProduct.getSellingPrice());
							record.setListID(agentProduct.getListID());
						}
						listProductResult.add(record);
					}
					listSubCat = productManager.listSubCatInfo();
					productModel.setListProduct(listProductResult);
					productModel.setAutoComplete(productManager.getAutoCompleteList());
				}
			}
			listSubCat = productManager.listSubCatInfo();
			return SUCCESS;
		} else {
			return "banned";
		}
	}

	// update categories
	public String editCategories() {
		productModel.setErrorAddCat("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null
				|| session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {
				if (productModel.getCategories() != null) {
					Categories info = productModel.getCategories();
					if(info.getCatName().equals("")){
						productModel.setErrorAddCat("Vui lòng điền tên danh mục");
					}else{
						if(productManager.checkCategories(info.getCatName()) && !productManager.checkCurrentCatName(info)){
							productModel.setErrorAddCat("Tên danh mục đã tồn tại");
						}else{
							productManager.updateCategories(info);
							productModel.setErrorAddCat("Thay đổi chi tiết danh mục thành công");
						}
					}
				}
				productModel.setListSubcat(productManager.listSubCatInfo());
				productModel.setListMainCategories(productManager.getAllMainCategories());
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// get all main categories, sub-categories
	public void listAllCategories() {
		productModel.setListSubcat(productManager.listSubCatInfo());
		productModel.setListMainCategories(productManager.getAllMainCategories());
	}

	// add new Main Categories
	public String addMainCategories(){
		productModel.setErrorAddCat("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {
	
			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {

				MainCategories mainCategories = productModel.getMainCategories();
				if(productModel.getMainCategories() == null){
					productModel.setErrorAddCat("Vui lòng điền tên danh mục");
					productModel.setCategories(null);
				}else{
					if(productManager.checkMainCategories(mainCategories)){
						productModel.setErrorAddCat("Danh mục đã tồn tại");
					}else{
						productManager.addMainCategories(mainCategories);
						productModel.setErrorAddCat("Thêm danh mục thành công");
					}
				}
					productModel.setListMainCategories(productManager.getAllMainCategories());
					productModel.setMainCategories(null);
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}
	
	// edit main categories
			public String editMainCategories() {
				productModel.setErrorAddCat("");
				Map<String, Object> session = ActionContext.getContext().getSession();
				if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

					if (session.get("role") == null) {
						return "login";
					} else if (session.get("role").equals(1)
							|| session.get("role").equals(2)) {
						MainCategories mainCategories = productModel.getMainCategories();
						if(mainCategories.getMainCatName().equals("")){
							productModel.setErrorAddCat("Vui lòng điền tên danh mục");
						}else{
							if(productManager.checkMainCategories(mainCategories)){
								productModel.setErrorAddCat("Tên danh mục đã tồn tại");
							}else{
								productManager.editMainCategories(mainCategories);
								productModel.setErrorAddCat("Thay đổi chi tiết danh mục thành công");
							}
						}
						productModel.setListMainCategories(productManager.getAllMainCategories());
						return "success";
					} else {
						return ERROR;
					}
				} else {
					return "banned";
				}
			}
			
			
	// view main categories
		public String viewMainCategories() {
			productModel.setErrorAddCat("");
			Map<String, Object> session = ActionContext.getContext().getSession();
			if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

				if (session.get("role") == null) {
					return "login";
				} else if (session.get("role").equals(1) || session.get("role").equals(2)) {
					String mainCatID = request.getParameter("mainCatID");
					if(mainCatID != null){
						productModel.setMainCategories(productManager.viewMainCategories(Integer.parseInt(mainCatID)));
					}
					productModel.setListMainCategories(productManager.getAllMainCategories());
					return "success";
				} else {
					return ERROR;
				}
			} else {
				return "banned";
			}
		}
		
	// view categories
	public String viewCategories() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {
				String id = request.getParameter("catID");
				if (id != null) {
					Integer catID = Integer.parseInt(id);
					if (catID != null) {
						productModel.setCategories(productManager.getCategories(catID));
					}
				}
				listAllCategories();
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// add new categories
	public String addCategories() {
		productModel.setErrorAddCat("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {

				if (productModel.getCategories() != null) {
					Categories cat = productModel.getCategories();
					if(cat.getCatName().equals("") || cat.getMainCatID().equals(0)){
						listAllCategories();
						productModel.setErrorAddCat("Vui lòng điền đầy đủ thông tin");
					}else{
						if (!productManager.checkCategories(cat.getCatName())) {
							productManager.insertCategories(cat);
							listAllCategories();
							productModel.setCategories(null);
							productModel.setErrorAddCat(null);
							productModel.setErrorAddCat("Thêm danh mục thành công");
						} else {
							listAllCategories();
							productModel.setErrorAddCat("Danh mục đã tồn tại");
							productModel.setCategories(null);
						}
					}
				}
				return "success";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}

	}

	// update Brand Detail
	public String editBrand() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {

				BrandInfo info = productModel.getBrandInfo();
				if (info != null) {
					productManager.updateBrand(info.getBrandID(), info.getBrandName(), info.getWebsite());
					listAllBrand();
				}
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	public String getBrandInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {
				String id = request.getParameter("id");
				if (id != null) {
					Integer brandId = Integer.parseInt(id);
					if (brandId != null) {
						BrandInfo info = productManager.getBrandInfo(brandId);
						if (info != null) {
							productModel.setBrandInfo(info);
						}
					}
				}
				listAllBrand();

				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	// list all Brand
	public void listAllBrand() {
		List<BrandInfo> listBrandInfo = productManager.getListBrand();
		if (listBrandInfo != null) {
			productModel.setListBrandInfo(listBrandInfo);
		}
	}

	public String addBrand() {
		productModel.setErrorAddBrand("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1) || session.get("role").equals(2)) {
				try {
					productModel.setErrorAddBrand(null);
					BrandInfo info = productModel.getBrandInfo();
					if (!info.getBrandName().equals("") && !info.getWebsite().equals("")) {
						if (!productManager.checkBrandName(info.getBrandName())) {
							productManager.addBrand(info.getBrandName(), info.getWebsite());
							listAllBrand();
							productModel.setBrandInfo(null);
							productModel.setErrorAddBrand("Thêm nhà sản xuất thành công");
						} else {
							productModel.setErrorAddBrand("Brand đã tồn tại");
							listAllBrand();
						}
					}else {
						productModel.setErrorAddBrand("Tên nhà sản xuất và website không được trống");
						listAllBrand();
					}
				} catch (Exception e) {
				}
				productModel.setBrandID(null);
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	public void addProductPageInfo() {
		List<SubCatInfo> listSubcat = productManager.listSubCatInfo();
		productModel.setListSubcat(listSubcat);
		List<BrandInfo> brandInfo = productManager.getListBrand();
		if (brandInfo != null) {
			productModel.setListBrandInfo(brandInfo);
		}
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	@Override
	public ProductModel getModel() {
		return productModel;
	}

	public ProductModel getProductModel() {
		return productModel;
	}

	public void setProductModel(ProductModel productModel) {
		this.productModel = productModel;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<SubCatInfo> getListSubCat() {
		return listSubCat;
	}

	public void setListSubCat(List<SubCatInfo> listSubCat) {
		this.listSubCat = listSubCat;
	}

	public ShopManager getShopManager() {
		return shopManager;
	}

	public void setShopManager(ShopManager shopManager) {
		this.shopManager = shopManager;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public List<String> getListAutoComplete() {
		return listAutoComplete;
	}

	public void setListAutoComplete(List<String> listAutoComplete) {
		this.listAutoComplete = listAutoComplete;
	}
}
